package com.lrkj.waitress.task;


import com.lrkj.common.utils.LrkjDateUtil;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.waitress.domain.CruiserTask;
import com.lrkj.waitress.domain.PathDetail;
import com.lrkj.waitress.service.ICruiserTaskService;
import com.lrkj.waitress.service.IPathDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 定时添加巡检任务
 */
@Component("pathTask")
public class pathTask {

        @Autowired
        private IPathDetailService pathDetailService;

        @Autowired
        private ICruiserTaskService cruiserTaskService;


        /**
         * 每天定时生成任务
         */
        public void eachCruiserTask(){
            PathDetail pathDetail=new PathDetail();
            pathDetail.setPatrolDate("1");
            //定时状态
            pathDetail.setCrulserType("10");
            //查询出每天定时的列表
            List<PathDetail> pathDetailList = pathDetailService.selectPathDetailList(pathDetail);
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            pathDetailList.stream().forEach(x->{
                CruiserTask cruiserTask=new CruiserTask();

                cruiserTask.setId(IdUtils.simpleUUID());
                cruiserTask.setCommunityId(x.getCommunityId());
                cruiserTask.setPathId(x.getPathId());
                //巡逻开始时间
                cruiserTask.setCruiserTime(sdf.format(new Date())+" "+x.getCrulserBegin()+":00");
                //巡逻结束时间
                cruiserTask.setCruiserEndtime(sdf.format(new Date())+" "+x.getCrulserEnd()+":00");
                //巡逻人
                cruiserTask.setCruiserPersion(x.getCrulserId());

                cruiserTask.setTaskState("10");
                cruiserTaskService.insertCruiserTask(cruiserTask);

            });
        }


            /**
             * 每周定时生成任务
             */
            public void weeklyCruiserTask(){
                PathDetail pathDetail=new PathDetail();
                pathDetail.setPatrolDate("2");
                //定时状态
                pathDetail.setCrulserType("10");
                //查询出每天定时的列表
                List<PathDetail> pathDetailList = pathDetailService.selectPathDetailList(pathDetail);
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                pathDetailList.stream().forEach(x->{
                    CruiserTask cruiserTask=new CruiserTask();

                    cruiserTask.setId(IdUtils.simpleUUID());
                    cruiserTask.setCommunityId(x.getCommunityId());
                    cruiserTask.setPathId(x.getPathId());
                    //巡逻开始时间
                    cruiserTask.setCruiserTime(sdf.format(new Date())+" "+x.getCrulserBegin()+":00");
                    //巡逻结束时间
                    cruiserTask.setCruiserEndtime(getFetureDate(6)+" "+x.getCrulserEnd()+":00");
                    //巡逻人
                    cruiserTask.setCruiserPersion(x.getCrulserId());

                    cruiserTask.setTaskState("10");
                    cruiserTaskService.insertCruiserTask(cruiserTask);

                });
            }

        /**
         * 每月定时生成任务
         */
        public void onceCruiserTask(){
            PathDetail pathDetail=new PathDetail();
            pathDetail.setPatrolDate("3");
            //定时状态
            pathDetail.setCrulserType("10");
            //查询出每天定时的列表
            List<PathDetail> pathDetailList = pathDetailService.selectPathDetailList(pathDetail);
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            pathDetailList.stream().forEach(x->{
                CruiserTask cruiserTask=new CruiserTask();

                cruiserTask.setId(IdUtils.simpleUUID());
                cruiserTask.setCommunityId(x.getCommunityId());
                cruiserTask.setPathId(x.getPathId());
                //巡逻开始时间
                cruiserTask.setCruiserTime(sdf.format(new Date())+" "+x.getCrulserBegin()+":00");
                //巡逻结束时间
                cruiserTask.setCruiserEndtime(getLastDayOfMonth(LrkjDateUtil.getCurrentMonth())+" "+x.getCrulserEnd()+":00");
                //巡逻人
                cruiserTask.setCruiserPersion(x.getCrulserId());

                cruiserTask.setTaskState("10");
                cruiserTaskService.insertCruiserTask(cruiserTask);
            });
        }



    /**
     * 获得该月最后一天
     *
     * @param
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int month) {
        Calendar cal = Calendar.getInstance();
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay=0;
        //2月的平年瑞年天数
        if(month==2) {
            lastDay = cal.getLeastMaximum(Calendar.DAY_OF_MONTH);
        }else {
            lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }


    /**
     * 获取未来 第 past 天的日期
     * @param past
     * @return
     */
    public static String getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }
}
